package com.mabs.ground.client.main.ui;

import java.util.ArrayList;
import java.util.List;

import com.github.gwtbootstrap.client.ui.NavLink;
import com.github.gwtbootstrap.client.ui.NavTabs;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.mabs.ground.client.common.MyPlace;

/**
 * @author <a href="Email: mohamedarbi.benslimane@allence-tunisie.com">Ben Slimane Arbi</a>
 * @version 1.0
 * @since 1.0 - 25 juil. 2014 - 20:09:57
 */
public class Main extends Composite {

	interface MyUiBinder extends UiBinder<Widget, Main> {
	}

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
	private PlaceController placeController;
	@UiField
	NavTabs navbar;
	@UiField
	SimplePanel mainPanel;
	final List<NavLink> navLink = new ArrayList<NavLink>();

	public Main(PlaceController placeController) {
		this.placeController = placeController;
		initWidget(uiBinder.createAndBindUi(this));

	}

	public NavTabs getNavbar() {
		return navbar;
	}

	public void setMainPanel(Widget w) {
		mainPanel.setWidget(w);
		updateNavigation();
	}

	public void updateNavigation() {
		Place place = placeController.getWhere();
		if (place instanceof MyPlace) {
			MyPlace myPlace = (MyPlace) place;
			for (NavLink n : Main.this.navLink) {
				n.setActive(false);
				if (n.getText().trim().compareTo(myPlace.getNavigationTitle().trim())==0) {
					n.setActive(true);
				}
			}
		}
	}

	public void addNavPlace(final MyPlace place) {
		final NavLink navLink = new NavLink(place.getNavigationTitle());
		navLink.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				placeController.goTo(place);
			}
		});
		navbar.add(navLink);
		this.navLink.add(navLink);
	}
}
